package com.dxg.msns.admin.properties;

import com.dxg.msns.auth.utils.RsaUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

@ConfigurationProperties(prefix = "msns.admin.jwt")
public class AdminJwtProperties {

        private String secret;
        private String pubKeyPath;
        private String priKeyPath;
        private int expire; //token过期时间
        private PublicKey publicKey; //公钥
        private PrivateKey privateKey; //私钥
        private String cookieName;
        private Integer cookieMaxAge;

        /**
         * @PostContruct：在构造方法执行之后执行该方法
         */
        @PostConstruct
        public void init(){
            try {
                File pubKey = new File(pubKeyPath);
                File priKey = new File(priKeyPath);
                if (!pubKey.exists() || !priKey.exists()) {
                    // 生成公钥和私钥
                    RsaUtils.generateKey(pubKeyPath, priKeyPath, secret);
                }
                // 获取公钥和私钥
                this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
                this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
            } catch (Exception e) {
                System.err.println("初始化公钥和私钥失败！");
                throw new RuntimeException("初始化公钥和私钥失败！");
            }
        }

        public String getSecret() {
            return secret;
        }

        public void setSecret(String secret) {
            this.secret = secret;
        }

        public String getPubKeyPath() {
            return pubKeyPath;
        }

        public void setPubKeyPath(String pubKeyPath) {
            this.pubKeyPath = pubKeyPath;
        }

        public String getPriKeyPath() {
            return priKeyPath;
        }

        public void setPriKeyPath(String priKeyPath) {
            this.priKeyPath = priKeyPath;
        }

        public int getExpire() {
            return expire;
        }

        public void setExpire(int expire) {
            this.expire = expire;
        }

        public PublicKey getPublicKey() {
            return publicKey;
        }

        public void setPublicKey(PublicKey publicKey) {
            this.publicKey = publicKey;
        }

        public PrivateKey getPrivateKey() {
            return privateKey;
        }

        public void setPrivateKey(PrivateKey privateKey) {
            this.privateKey = privateKey;
        }

        public String getCookieName() {
            return cookieName;
        }

        public void setCookieName(String cookieName) {
            this.cookieName = cookieName;
        }

        public Integer getCookieMaxAge() {
            return cookieMaxAge;
        }

        public void setCookieMaxAge(Integer cookieMaxAge) {
            this.cookieMaxAge = cookieMaxAge;
        }


}
